package com.example.mbl.service.impl;

import com.example.mbl.dao.AlumniMapper;
import com.example.mbl.dao.StudentMapper;
import com.example.mbl.dto.StudentDto;
import com.example.mbl.entity.Alumni;
import com.example.mbl.entity.Student;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.AlumniPageResult;
import com.example.mbl.result.business.StudentPageResult;
import com.example.mbl.service.StudentService;
import com.example.mbl.util.Tools;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Spliterator;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private AlumniMapper alumniMapper;

    @Override
    public WebRestResult addStudent(Student student) {
        WebRestResult result = new WebRestResult();
        if(Tools.isEmpty(student.getFlag()))
            student.setFlag("0");
        student.setStudentid(UUIDUtil.randomUUID());
        if(studentMapper.insert(student)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增学生失败");
        }
        return result;
    }

    @Override
    public WebRestResult addStudentBatch(StudentDto studentDto) {
        WebRestResult result = new WebRestResult();
        if(studentDto.getStudentList().size()==0){
            result.setResult(WebRestResult.SUCCESS);
            return result;
        }
        List<Student> studentList = studentDto.getStudentList();
        for(int i=0 ; i<studentList.size() ; i++){
            studentList.get(i).setFlag("0");
            studentList.get(i).setStudentid(UUIDUtil.randomUUID());
        }
        studentDto.setStudentList(studentList);
        if(studentMapper.insertStudentBatch(studentDto)==studentList.size()){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增学生失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteStudent(Student student) {
        WebRestResult result = new WebRestResult();
        if(studentMapper.deleteByPrimaryKey(student.getStudentid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除学生失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateStudent(Student student) {
        WebRestResult result = new WebRestResult();
        if(studentMapper.updateByPrimaryKeySelective(student)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("修改学生失败");
        }
        return result;
    }

    @Override
    public WebRestResult setStudentVisible(Student student) {
        WebRestResult result = new WebRestResult();
        if(studentMapper.setStudentVisible(student.getStudentid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("设置失败");
        }
        return result;
    }

    @Override
    public PageResult<StudentPageResult> getStudentList(String year, String degree, int page, int pagesize) {
        PageResult<StudentPageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;

        List<StudentPageResult> list = studentMapper.getStudentList(year,degree,start,pagesize);
        if(list.size()>0){
            result.setPageCount(list.get(0).getPage());
            result.setTotal(list.get(0).getCnt());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }

    @Override
    public WebRestResult addAlumni(Alumni alumni) {
        WebRestResult result = new WebRestResult();
        alumni.setAlumniid(UUIDUtil.randomUUID());
        if(Tools.isEmpty(alumni.getFlag()))
            alumni.setFlag("0");
        if(alumniMapper.insert(alumni)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增校友失败");
        }
        return result;
    }

    @Override
    public WebRestResult addAlumniBatch(StudentDto studentDto) {
        WebRestResult result = new WebRestResult();
        if(studentDto.getAlumniList().size()==0){
            result.setResult(WebRestResult.SUCCESS);
            return result;
        }
        List<Alumni> alumniList = studentDto.getAlumniList();
        for(int i=0;i<alumniList.size();i++){
            alumniList.get(i).setAlumniid(UUIDUtil.randomUUID());
            alumniList.get(i).setFlag("0");
        }
        studentDto.setAlumniList(alumniList);
        if(alumniMapper.insertAlumniBatch(studentDto)==alumniList.size()){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增校友失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteAlumni(Alumni alumni) {
        WebRestResult result = new WebRestResult();
        if(alumniMapper.deleteByPrimaryKey(alumni.getAlumniid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除校友失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateAlumni(Alumni alumni) {
        WebRestResult result = new WebRestResult();
        if(alumniMapper.updateByPrimaryKey(alumni)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("修改校友失败");
        }
        return result;
    }

    @Override
    public WebRestResult setAlumniVisible(Alumni alumni) {
        WebRestResult result = new WebRestResult();
        if(alumniMapper.setAlumniVisible(alumni.getAlumniid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("设置失败");
        }
        return result;
    }

    @Override
    public PageResult<AlumniPageResult> getAlumniList(String year, int page, int pagesize) {
        PageResult<AlumniPageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;

        List<AlumniPageResult> list = alumniMapper.getAlumniList(year,start,pagesize);
        if(list.size()>0){
            result.setPageCount(list.get(0).getPage());
            result.setTotal(list.get(0).getCnt());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }
}
